//+------------------------------------------------------------------+
//|                                                  get 3 level.mq4 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, Pham Thai Son."
#property link      "http://www.future.is.now"

/*
#import "kernel32.dll"
   void OutputDebugStringA(string msg);
#import
*/
//---- input parameters 
extern double Period1=0; 
extern double Period2=0; 
extern double Period3=65; 
extern string   Dev_Step_1="1,3";
extern string   Dev_Step_2="8,5";
extern string   Dev_Step_3="21,12";
extern int Symbol_1_Kod=140;
extern int Symbol_2_Kod=141;
extern int Symbol_3_Kod=142;
extern double Lots = 0.1;
extern double macdThresold = 0.00015;
// elliot WAVE
extern int elliot_wave = 0;
// macd signal
int macd_signal = 0;

int MinStopDist;
double HighCurrent, LowCurrent;
// store peak point array
double pointValue[10000];
int pointType[10000]; // 4 is peak low, 5 is peak high, 2 is medium low, 3 is medium high
int pointBar[10000];  // store bar  of peak point
int pointTicket[10000]; // store 
int pointCount = 0;
//---- 3 level semafor variable
double ZZ_0, ZZ_1, ZZ_2, ZZ_3, ZZ_4, ZZ_5;
//---- MACD variable
double MacdCurrent, MacdPrevious, SignalCurrent;
double SignalPrevious, MaCurrent, MaPrevious;
// now we will do many order by the trend strength not 1 any more 
// huge profit is here when market has strong trend :)
int orderCount = 0;
int total = 0;
int cnt = 0;
int lastTradePoint = 0;
int ticket;
// 2 holly line
double high_line;
double low_line;
double tmp_line;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   MinStopDist = MarketInfo(Symbol(), MODE_STOPLEVEL);
   //hma_1=iCustom(Symbol(),0,"HMA",40,0,3,1,0);
   //hma_2=hma_1;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
  
/*  
void dump(){
   for(int i = 1; i <= pointCount; i++){
      OutputDebugStringA("pointType = " + pointType[i]);
      OutputDebugStringA("pointValue = " + pointValue[i]);
      OutputDebugStringA("index = " + i);
      OutputDebugStringA("AAAAAAAA");      
   }
   OutputDebugStringA("XXXXXXXXXXXXXXXXXXXXXXXXX");
   OutputDebugStringA("XXXXXXXXXXXXXXXXXXXXXXXXX");
}
*/
//+------------------------------------------------------------------+
//| expert start function                                 |
//+------------------------------------------------------------------+
int start()
  {   
// initial data checks
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }  
// get macd
/*
   MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
   SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); 
   
   if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious && MathAbs(MacdCurrent) >= macdThresold){
      macd_signal = 1;
   }
   if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious && MathAbs(MacdCurrent) >= macdThresold){
      macd_signal = -1;
   }
*/
//---- get 3 level zz semafor
   //ZZ_2=iCustom(Symbol(),0,"3_Level_ZZ_Semafor",Period1,Period2,Period3,Dev_Step_1,Dev_Step_2,Dev_Step_3,Symbol_1_Kod,Symbol_2_Kod,Symbol_3_Kod,2,0);
   //ZZ_3=iCustom(Symbol(),0,"3_Level_ZZ_Semafor",Period1,Period2,Period3,Dev_Step_1,Dev_Step_2,Dev_Step_3,Symbol_1_Kod,Symbol_2_Kod,Symbol_3_Kod,3,0);
   ZZ_4=iCustom(Symbol(),0,"3_Level_ZZ_Semafor",Period1,Period2,Period3,Dev_Step_1,Dev_Step_2,Dev_Step_3,Symbol_1_Kod,Symbol_2_Kod,Symbol_3_Kod,4,0);
   ZZ_5=iCustom(Symbol(),0,"3_Level_ZZ_Semafor",Period1,Period2,Period3,Dev_Step_1,Dev_Step_2,Dev_Step_3,Symbol_1_Kod,Symbol_2_Kod,Symbol_3_Kod,5,0);
//---- update 3 level peak point if reach
   if(ZZ_4 > 0) {  
      //Print("Peak low reached");        // if peak low detect we must update previous 
      //dump();
      if(pointType[pointCount] != 4)
       {
         pointCount++;                  
       }
      pointType[pointCount] = 4;
      pointValue[pointCount] = ZZ_4; 
      pointBar[pointCount] = Bars;  
      pointTicket[pointCount] = 0; 
   }  
   if(ZZ_5 > 0)  { 
      //Print("Peak high reached");
      //dump();
      if(pointType[pointCount] != 5)
       {
         pointCount++;        
       }
      pointType[pointCount] = 5;
      pointValue[pointCount] = ZZ_5;
      pointBar[pointCount] = Bars;
      pointTicket[pointCount] = 0;
   }
   if(pointCount > 3){
      // draw point previous -1 and -3 point
      tmp_line = pointValue[pointCount-1] + ((pointValue[pointCount-1] - pointValue[pointCount-3]) * (Bars - pointBar[pointCount -1])) / (pointBar[pointCount -1] - pointBar[pointCount -3]);
      if(pointType[pointCount-1] == 4)low_line = tmp_line;
      if(pointType[pointCount-1] == 5)high_line = tmp_line;
      if(pointCount > 4){
         tmp_line = pointValue[pointCount-2] + ((pointValue[pointCount-2] - pointValue[pointCount-4]) * (Bars - pointBar[pointCount -2])) / (pointBar[pointCount -2] - pointBar[pointCount -4]);
         if(pointType[pointCount-2] == 4)low_line = tmp_line;
         if(pointType[pointCount-2] == 5)high_line = tmp_line;
      }
      //OutputDebugStringA("low_line = " + low_line);
      //OutputDebugStringA("high_line = " + high_line);
      //OutputDebugStringA("XXXXXXXXX");
      if(high_line > 0 && Ask > high_line + 1000* Point&& lastTradePoint != pointCount){
      // then buy
         close_sell();
         place_buy();
         lastTradePoint = pointCount;               
      }
      if(low_line > 0 && Bid < low_line -1000* Point && lastTradePoint != pointCount){
      // then sell  
         close_buy();
         place_sell();
         lastTradePoint = pointCount;   
      }
      
   }

 
   return(0);   
  }

//+------------------------------------------------------------------+
//| expert place buy function                                 |
//+------------------------------------------------------------------+
void place_buy()
  {
      total=OrdersTotal();
      if(total ==0)
       {
         Lots = MathRound(AccountBalance()/10000 ) * 0.1;
      
         ObjectCreate("Arrow" + Bid, OBJ_ARROW, 0, TimeCurrent(), Bid); //draw an up arrow
         ObjectSet("Arrow" + Bid, OBJPROP_STYLE, STYLE_SOLID);
         ObjectSet("Arrow" + Bid, OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
         ObjectSet("Arrow" + Bid, OBJPROP_COLOR,Green);
         ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0 ,"macd sample",16384,0,Green); // stoploss at current low               
         pointTicket[pointCount] = ticket;
      }
  } 
//+------------------------------------------------------------------+
//| expert place sell function                                 |
//+------------------------------------------------------------------+
void place_sell()
  {
      total=OrdersTotal();
      if(total == 0)
       {
         Lots = MathRound(AccountBalance()/10000) * 0.1;
      // too close to pit point so do not place order
      
         ObjectCreate("Arrow" + Ask, OBJ_ARROW, 0, TimeCurrent(), Ask); //draw an down arrow
         ObjectSet("Arrow" + Ask, OBJPROP_STYLE, STYLE_SOLID);
         ObjectSet("Arrow" + Ask, OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
         ObjectSet("Arrow" + Ask, OBJPROP_COLOR,Red); 
         ticket = OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0 ,"macd sample",16384,0,Red); // stoploss at current hi
         pointTicket[pointCount] = ticket;
       }
  }
//+------------------------------------------------------------------+
//| expert close buy trade function                                 |
//+------------------------------------------------------------------+
void close_buy()
 {
   total=OrdersTotal();
//---- if there is 1 order then signal is changed we take profit.
//---- now we scan all order, see if an order should be closed.
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()==OP_BUY &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {        
           OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position        
        } 
     }
 }
 
 //+------------------------------------------------------------------+
//| expert close sell trade function                                 |
//+------------------------------------------------------------------+
void close_sell()
 {
   total=OrdersTotal();
//---- if there is 1 order then signal is changed we take profit.
//---- now we scan all order, see if an order should be closed.
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);             
      if(OrderType()==OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {        
           OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position   
        }
     }
 }

